<?php

namespace App\Http\Controllers\BusinessData;

use App\Http\Conf\ResultInfo;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Models\Business\InvoiceTargetKeyProduct;
use App\Http\Models\CategoryKeyProduct;
use App\Http\Models\SalesRegion;
use App\Http\Requests\Business\InvoiceTargetKeyProductRequest;

class InvoiceTargetKeyProductController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $where = [];
        is_numeric($request->year) ? $where[] = ['year', $request->year] : '';
        is_numeric($request->sales_region_code) ? $where[] = ['sales_region_code', $request->sales_region_code] : '';
        is_numeric($request->category_key_product_id) ? $where[] = ['category_key_product_id', $request->category_key_product_id] : '';
        $query = InvoiceTargetKeyProduct::where($where);

        $total_quantity = $query->sum('quantity');
        $models = $query->paginate(config('report.quantity_per_page'));
        $sales_regions = SalesRegion::all();
        $category_key_products = CategoryKeyProduct::all();

        return view('business_data.invoice_target_key_product.index', [
            'models' => $models,
            'sales_regions' => $sales_regions,
            'total_quantity' => $total_quantity,
            'category_key_products' => $category_key_products,
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $sales_regions = SalesRegion::all();
        $category_key_products = CategoryKeyProduct::all();
        return view('business_data.invoice_target_key_product.create', [
            'sales_regions' => $sales_regions,
            'category_key_products' => $category_key_products,
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(InvoiceTargetKeyProductRequest $request)
    {
        //判断是否存在数据
        $model_exists = InvoiceTargetKeyProduct::where([
            'year' => $request->year,
            'sales_region_code' => $request->sales_region_code,
            'category_key_product_id' => $request->category_key_product_id,
        ])->first();

        if ($model_exists) {
            return redirect()->back()->with('fail', '该指标已经存在数据!');
        }

        $model = new InvoiceTargetKeyProduct();
        $model->year = $request->year;
        $model->sales_region_code = $request->sales_region_code;
        $model->category_key_product_id = $request->category_key_product_id;
        $model->quantity = $request->quantity;
        if ($model->save()) {
            return redirect()->action('BusinessData\InvoiceTargetKeyProductController@show', $model);
        } else {
            dd("添加数据有异常！");
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $model = InvoiceTargetKeyProduct::find($id);
        return view('business_data.invoice_target_key_product.show', ['model' => $model]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $sales_regions = SalesRegion::all();
        $category_key_products = CategoryKeyProduct::all();
        $model = InvoiceTargetKeyProduct::find($id);
        return view('business_data.invoice_target_key_product.edit', [
            'sales_regions' => $sales_regions,
            'category_key_products' => $category_key_products,
            'model' => $model
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(InvoiceTargetKeyProductRequest $request, $id)
    {
        $model = InvoiceTargetKeyProduct::find($id);
        $model->year = $request->year;
        $model->sales_region_code = $request->sales_region_code;
        $model->category_key_product_id = $request->category_key_product_id;
        $model->quantity = $request->quantity;
        if ($model->save()) {
            return redirect()->action('BusinessData\InvoiceTargetKeyProductController@show', $model);
        } else {
            dd("编辑数据有异常！");
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $model = InvoiceTargetKeyProduct::find($id);
        if ($model->delete()) {
            return ['status' => ResultInfo::SUCCESS];
        } else {
            return ['status' => ResultInfo::UNKNOWN];
        }
    }
}
